<?php

namespace App\Admin\Controllers;

use App\Models\Area;
use App\Models\Complaint;
use App\Models\Scene;
use DB;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Show;

class HandledController extends Controller
{
    use HasResourceActions;

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header('旅游投诉');

            $content->body($this->grid());
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Show interface.
     *
     * @param mixed   $id
     * @param Content $content
     * @return Content
     */
    public function show($id, Content $content)
    {
        return $content
            ->header('详情')
            ->body($this->detail($id));
    }

    /**
     * Make a show builder.
     *
     * @param mixed   $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(Complaint::findOrFail($id));
        $show->area_id('区域')->using(Area::all()->pluck('title','id')->toArray());
        $show->type('事件类型')->using(config('options.complaint_type'));
        $show->target('投诉类型')->using(config('options.credit_module'))->load('target_id', '/api/target_id');
        $show->target_id('投诉对象')->using(Scene::all()->pluck('title','id')->toArray());
        $show->behavior('投诉行为')->using(config('options.complaint_behavior'));
        $show->other('其他')->help('投诉类型为其他时选填此项');
        $show->name('投诉人');
        $show->phone('联系电话');
        $show->cause('投诉事由/诉求');
        $show->time('投诉日期');
        $show->handle_person('值班人员');
        $show->enforcement_number('执法证编号');
        $show->result('处理结果');
        $show->handle_time('处理日期');
        $show->result_status('游客满意度')->using(config('options.complaint_result'));
        $show->status('状态')->using(config('options.complaint_status'));

        return $show;
    }
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(Complaint::class, function (Grid $grid) {
            $grid->actions(function ($actions) {
                $actions->disableEdit();
            });
            $grid->disableCreateButton();
            $grid->disableExport();
            $grid->model()->where('status', 2);
            $grid->id('ID')->sortable();
            $grid->name('投诉人');
            $grid->phone('联系电话');
            $grid->time('投诉时间');
            $grid->column('target','投诉类型')->display(function ($val){
                return $val?config('options.credit_module')[$val]:'';
            });
            $grid->target_id('投诉对象')->display(function ($val){
                $data = DB::table($this->target)->find((int)$val);
                return $val?($this->target=='guides'?$data->name:$data->title):'';
            });
            $grid->behavior('投诉行为')->display(function ($val){
                return config('options.complaint_behavior')[$val];
            });
            $grid->handle_person('值班人员');
            $grid->result_status('游客满意度')->display(function ($val){
                return $val!=null?config('options.complaint_result')[$val]:'';
            });
            $grid->status()->radio(config('options.complaint_status'));
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::form(Complaint::class, function (Form $form) {
//            $form->tab('投诉信息',function ($form){
//                $form->select('area_id','区域')->options(Area::all()->pluck('title','id'));
//                $form->select('type','事件类型')->options(config('options.complaint_type'));
//                $form->select('target','投诉类型')->options(config('options.credit_module'))->load('target_id', '/api/target_id')->default(null);
//                $form->select('target_id','投诉对象')->options(Scene::all()->pluck('title','id'));
//                $form->select('behavior','投诉行为')->options(config('options.complaint_behavior'));
//                $form->text('other','其他')->help('投诉类型为其他时选填此项');
//                $form->text('name','投诉人');
//                $form->text('phone','联系电话');
//                $form->textarea('cause','投诉事由/诉求');
//                $form->date('time','投诉日期');
//            })->tab('投诉处理',function ($form){
//                $form->text('handle_person','值班人员');
//                $form->text('enforcement_number','执法证编号');
//                $form->textarea('result','处理结果');
//                $form->date('handle_time','处理日期');
//                $form->select('result_status','游客满意度')->options(config('options.complaint_result'));
//                $form->radio('status','状态')->options(config('options.complaint_status'));
//            });
            $form->hidden('status','状态');
        });
    }
}
